import axios from 'axios'
const instance = axios.create({
  timeout: 10000
})
instance.interceptors.request.use(
  (config) => {
    const token = sessionStorage.getItem("token")
    if (token) {
      config.headers.Authorization = token
    }
    return config
  },
  (err) => Promise.reject(err)
)

instance.interceptors.response.use(
  (res) => {
    if (res.data.code === 401) {
      ElMessage.error('参数有误')
    }
    return res.data
  },
  (err) => {
    if (err.response.data.code === 406) {
      ElMessage.error('权限不通，请重新登录')
      localStorage.removeItem("token");
      sessionStorage.removeItem("token");
      sessionStorage.removeItem("userInfo")
      location.pathname = "/"
      return;
    }
  }
)

export default instance